﻿<%@ Page Language="C#" AutoEventWireup="true" %>

<%@ Import Namespace="weixin.Models" %>
<%@ Import Namespace="weixin.Services" %>
<% 
    DomainService service = new DomainService();
    //string mappid = "wxfa43d527cfbd8ce5";//"直营店wx718c3d3a69668661";
    if (string.IsNullOrEmpty(Request.QueryString["code"]))
    {
        if (Request.UrlReferrer == null || Request.UrlReferrer.Host == null)
        {
            Response.Write("无效的来源");
            return;
        }
        tb_Domains model = service.GetByHost(Request.UrlReferrer.Host);
        if (model == null)
        {
            Response.Write("授权域名不存在");
            return;
        }
        Response.Redirect(string.Format("https://open.weixin.qq.com/connect/oauth2/authorize?appid={0}&redirect_uri={1}&response_type=code&scope={3}&state={4}&component_appid={2}#wechat_redirect",
                model.appid,
                System.Web.HttpUtility.UrlEncode(Request.Url.AbsoluteUri),
                weixin.api.WXConfig.sAppID,
                (model.scope == 1 ? "snsapi_userinfo" : "snsapi_base"),//"snsapi_base"
                model.state//Request.UrlReferrer != null && Request.UrlReferrer.Host == "farm.jmbbsact.cn" ? "farm" : ""
            ));
    }
    else
    {
        string state = Request["state"];
        tb_Domains model = service.GetByState(state);
        if (model == null)
        {
            Response.Redirect("/index.html");
        }
        string url = string.Format("https://api.weixin.qq.com/sns/oauth2/component/access_token?appid={0}&code={1}&grant_type=authorization_code&component_appid={2}&component_access_token={3}", model.appid, Request["code"], weixin.api.WXConfig.sAppID, weixin.api.WXComponent.GetComponentAccessToken(weixin.api.WXConfig.sAppID));
        System.Net.WebClient wc = new System.Net.WebClient();
        wc.Encoding = System.Text.Encoding.UTF8;
        string jsonstr = wc.DownloadString(url);
        if (model.scope == 1)
        {
            Newtonsoft.Json.Linq.JObject json = Newtonsoft.Json.Linq.JObject.Parse(jsonstr);
            string openid = json.GetValue("openid").ToString();
            url = string.Format("https://api.weixin.qq.com/sns/userinfo?access_token={0}&openid={1}&lang=zh_CN", json.GetValue("access_token"), openid);
            jsonstr = wc.DownloadString(url);
        }
        else
        {
            Newtonsoft.Json.Linq.JObject json = Newtonsoft.Json.Linq.JObject.Parse(jsonstr);
            jsonstr = "{\"openid\":\"" + json.GetValue("openid") + "\"}";
        }
        //weixin.api.WXComm.WriteLog(jsonstr);
        Response.Redirect(model.callback + (model.callback.Contains("?") ? "&" : "?") + model.jkey + "=" + System.Web.HttpUtility.UrlEncode(jsonstr));
        //if (state == "farm")
        //{
        //    Response.Redirect("http://farm.jmbbsact.cn/index.php?m=game&a=index&json=" + System.Web.HttpUtility.UrlEncode(jsonstr));
        //}

        //Response.Redirect(""+System.Web.HttpUtility.UrlEncode(jsonstr));
        //Session["openid"] = openid;
        //Response.Redirect("/crm/login.aspx");
    }
%>